package com.lagou.task.servlet;

import com.lagou.task.entity.Student;
import com.lagou.task.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "StudentFindServlet",urlPatterns = "/find")
public class StudentFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("selectById");
        if (id != null && !id.equals("")){
            StudentService studentService = new StudentService();
            Student student = studentService.findStudentService(id);
            if (student != null){
                request.getSession().setAttribute("findStudent",student);
                response.sendRedirect("find.jsp");
            }else {
                request.setAttribute("findError","findError");
                // 实现服务器跳转  共享request和response对象
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("show.jsp");
                requestDispatcher.forward(request, response);
            }
        }else {
            response.sendRedirect("show.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
